package com.example.leetcode;

public class Leetcode19 {

     public static class ListNode {
         int val;
         ListNode next;
         ListNode() {}
         ListNode(int val) { this.val = val; }
         ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     }

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode ret=new ListNode(0,head);
        ListNode last=head;
        ListNode pre=ret;

        a: while (last!=null){
            while (n>0){
                n--;
                if(last.next==null){
                    break a;
                }
                last=last.next;

            }

            pre=pre.next;
            last=last.next;
        }
        pre.next=pre.next.next;
        return ret.next;
    }
    public static void main(String[] args) {

        ListNode head1=new ListNode(1);
        ListNode head2=new ListNode(2);
       // head1.next=head2;


        ListNode listNode = removeNthFromEnd(head1, 1);
        System.out.println(listNode.val);
    }
}
